﻿Imports System.Data
Imports System.Data.SqlClient
Public Class Connect
    Private cmd As SqlCommand
    Private conn As SqlConnection
    Private stconnect As String = ("Data Source=NGOCANHPHAM-PC;Initial Catalog=KNM2012;Integrated Security=True")
    Public Function Connect() As Boolean
        conn = New SqlConnection(stconnect)
        Try
            conn.Open()
        Catch ex As Exception
            System.Windows.Forms.MessageBox.Show("Không thể kết nối đến Database! " + ex.Message)
        End Try
        Return True
    End Function
    Public Sub Close()
        If conn.State = ConnectionState.Open Then
            conn.Close()
        End If
        conn.Dispose()
    End Sub
    Public Function GetData(ByVal sSql As String) As DataTable
        Dim dt As New DataTable
        Connect()
        cmd = New SqlCommand(sSql, conn)
        Dim da As New SqlDataAdapter(cmd)
        da.Fill(dt)
        Close()
        Return dt
    End Function
    Public Function Excute(ByVal sSql As String)
        cmd = New SqlCommand()
        Connect()
        cmd.Connection = conn
        cmd.CommandText = sSql
        Return cmd.ExecuteNonQuery()
    End Function

End Class
